<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class NotificationsApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$allNotifications = $notificationTable->allNotifications()->toArray();
		if(count($allNotifications)!=0){
			return new JsonModel(array(
				'value'		    	=> 1,
				'allNotifications'  =>json_encode($allNotifications),
				
			));
		}else{
			return new JsonModel(array(
				'value'			=>	0,
			));
		}
    }
    public function get($type)
    {	
		
		header('Access-Control-Allow-Origin: *');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$explodedId=explode('-',$type);
		if($explodedId[0]==1){
				$getNotification = $notificationTable->getNotification($explodedId[1])->toArray();
				if(count($getNotification)!=0){
					$updateCountStatus=$notificationTable->updateCountStatus($getNotification);
				}
		}else if($explodedId[0]==2){
				$getNotification = $notificationTable->getAdminNotification($explodedId[1])->toArray();
				if(count($getNotification)!=0){
					$updateCountStatus=$notificationTable->updateCountStatus($getNotification);
				}
		}else if($explodedId[0]==3){
			$shopnotifications= $notificationTable->shopNotifications($explodedId[1])->toArray();
			if(count($shopnotifications)!=0){
				$getNotification = $notificationTable->getshopNotification($explodedId[1])->toArray();
				if(count($getNotification)!=0){
					$updateCountStatus=$notificationTable->updateCountStatus($getNotification);
				}
			}else{
				$getNotification = $notificationTable->getshopNotifications($explodedId[1])->toArray();
			}
		}else if($explodedId[0]==4){
			$getNotification = $notificationTable->getcountUserNotification($explodedId[1])->toArray();
		}else if($explodedId[0]==5){
			$getNotification = $notificationTable->getcountAdminNotification($explodedId[1])->toArray();
		}else if($explodedId[0]==6){
			$getNotification = $notificationTable->getcountShopNotification($explodedId[1])->toArray();
		}
		$count=count($getNotification);
		if(count($getNotification)!=0){
			return new JsonModel(array(
				'count'			=> $count,
				'notifications' => $getNotification
			));
		}else{
			return new JsonModel(array(
				'count'			=>	0,
			));
		}
    }
    public function create($usertype)
    {
		header('Access-Control-Allow-Origin: *');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		if($usertype['usertype']==2){
			$getAllUserNotifications = $notificationTable->getAllUserNotifications($usertype['user_id'])->toArray();
		}else if($usertype['usertype']==1){
			$getAllUserNotifications = $notificationTable->getAllAdminNotifications($usertype['user_id'])->toArray();
		}else{
			$getAllUserNotifications = $notificationTable->getAllShopNotifications($usertype['user_id'])->toArray();
		}
		if(count($getAllUserNotifications)!=0){
			return new JsonModel(array(
				'value'						=>	1,
				'noitifications'			=> 	$getAllUserNotifications,
			));
		}else{
			return new JsonModel(array(
				'status'		=> 'fail',
				'value'			=>	0,
			));
		}
    }
    public function update($order_id,$order)
    {
		header('Access-Control-Allow-Origin: *');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		if($order['updatetype']==1){
			$updateNotificationStatus = $notificationTable->updateNotificationStatus($order_id,$order);
		}else if($order['updatetype']==2){
			$updateNotificationStatus = $notificationTable->updateConforderNotStatus($order_id,$order);
		}else if($order['updatetype']==3){
			$changeNotificationStatus = $notificationTable->updatecomreview($order_id,$order);
		}else{
			$changeNotificationStatus = $notificationTable->changeNotificationStatus($order_id,$order);
		}
		
		return new JsonModel(array(
			'status'			=>	'success',
			'value'             =>  1,
		));	  
    }
    public function delete($id)
    {
       
    }
}